﻿using MyWPF.Models;
using MyWPF.Models.UserManagement;
using MyWPF.StaticData;
using MyWPF.ViewModel.Subpage;
using MyWPF.Views.Others;
using MyWPF.Views.Setting;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace MyWPF.ViewModel.User
{
    public class VM_UserWindow : BaseViewModel
    {
        public VM_UserWindow()
        {
            #region 事件委托
            _close = new DelegateCommand<object>(_Close);
            _min = new DelegateCommand<object>(_Min);
            _moveWindow = new DelegateCommand<object>(_MoveWindow);
            _login = new DelegateCommand<object>(_Login);
            _refresh = new DelegateCommand<object>(_Refresh);
            _addUser = new DelegateCommand<object>(_AddUser);
            _modifyUser = new DelegateCommand<object>(_ModifyUser);
            _deleteUser = new DelegateCommand<object>(_DeleteUser);
            #endregion

            #region 初始化
            ButtonText = "登录";
            #endregion
        }
        #region 基础：前端事件
        /// <summary>
        /// 关闭窗口
        /// </summary>
        private ICommand _close;
        public ICommand Close
        {
            get { return _close; }
            set
            {
                _close = value;
                OnPropertyChanged("Close");
            }
        }
        private void _Close(object obj)
        {
            StaticModels.userWindow.Close();
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        private ICommand _min;
        public ICommand Min
        {
            get { return _min; }
            set
            {
                _min = value;
                OnPropertyChanged("Min");
            }
        }
        private void _Min(object obj)
        {
            StaticModels.userWindow.WindowState = System.Windows.WindowState.Minimized;
        }
        /// <summary>
        /// 移动窗口
        /// </summary>
        private ICommand _moveWindow;
        public ICommand MoveWindow
        {
            get { return _moveWindow; }
            set
            {
                _moveWindow = value;
                OnPropertyChanged("MoveWindow");
            }
        }
        private void _MoveWindow(object obj)
        {
            StaticModels.userWindow.DragMove();
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string v)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
        }
        #endregion


        #region 属性绑定：下拉列表，用户下拉选择
        /// <summary>
        /// 当前页内容
        /// </summary>
        public ObservableCollection<User> TableData
        {
            get { return _tableData; }
            set
            {
                SetProperty(ref _tableData, value);
            }
        }
        private ObservableCollection<User> _tableData;

        /// <summary>
        /// 用户datatable，它与界面不绑定
        /// </summary>
        public DataTable UserDataTable
        {
            get { return _userDataTable; }
            set 
            {
                TableData = new ObservableCollection<User>();

                for (int i = 0; i < value.Rows.Count; i++)
                {
                    TableData.Add(new User()
                    {

                        UserName = Convert.ToString(value.Rows[i]["用户"]),
                        Power = Convert.ToString(value.Rows[i]["权限"]),
                        Remarks = Convert.ToString(value.Rows[i]["备注"]),
                    });
                }
                SetProperty(ref _userDataTable, value); 
            }
        }
        private DataTable _userDataTable;
        /// <summary>
        /// 用户选择行的内容
        /// </summary>
        public User SelectedUser
        {
            get
            {
                return _selectedUser;
            }
            set
            {
                _selectedUser = value;
                OnPropertyChanged("SelectedUser");

            }
        }
        private User _selectedUser;
        #endregion

        #region 属性绑定:当前用户名，登录按钮文本
        private string _currentUser;
        public string CurrentUser
        {
            get { return _currentUser; }
            set { SetProperty(ref _currentUser, value); }
        }
        private string _buttonText;
        public string ButtonText
        {
            get { return _buttonText; }
            set { SetProperty(ref _buttonText, value); }
        }
        #endregion

        #region 事件绑定：登录刷新增加用户等按钮
        /// <summary>
        /// 登录
        /// </summary>
        private ICommand _login;
        public ICommand Login
        {
            get { return _login; }
            set
            {
                _login = value;
                OnPropertyChanged("Login");
            }
        }
        private void _Login(object obj)
        {
            if (StaticModels.user.User.UserName == "" || StaticModels.user.User.UserName == null)
            {
                try
                {
                    StaticModels.vM_UserWindow_Login.PassWord = "";
                    StaticModels.vM_UserWindow_Login.Tips = "";

                    StaticModels.userWindow_Login = new Views.User.UserWindow_Login();
                    StaticModels.userWindow_Login.ShowDialog();
                }
                catch
                {
                    StaticModels.userWindow_Login = new Views.User.UserWindow_Login();
                    StaticModels.userWindow_Login.ShowDialog();
                }
            }
            else
            {
                //填写日志
                StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                {
                    Context = "用户主动退出登录",
                    Time = DateTime.Now,
                    Type = Models.Journal.JournalType.日志,
                    User = StaticModels.user.User.UserName
                });
                //退出权限和当前用户
                StaticModels.user.User = new Models.UserManagement.UserContext();
                //刷新用户管理主界面用户清单
                StaticModels.vM_UserWindow.UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);

                //用户界面刷新
                ButtonText = "登录"; ;
                CurrentUser = "";
            }
            
        }
        /// <summary>
        /// 刷新
        /// </summary>
        private ICommand _refresh;
        public ICommand Refresh
        {
            get { return _refresh; }
            set
            {
                _refresh = value;
                OnPropertyChanged("Refresh");
            }
        }
        private void _Refresh(object obj)
        {
            UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        private ICommand _addUser;
        public ICommand AddUser
        {
            get { return _addUser; }
            set
            {
                _addUser = value;
                OnPropertyChanged("AddUser");
            }
        }
        private void _AddUser(object obj)
        {
            StaticModels.userWindow_AddUser = new Views.User.UserWindow_AddUser();
            StaticModels.userWindow_AddUser.ShowDialog();
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        private ICommand _modifyUser;
        public ICommand ModifyUser
        {
            get { return _modifyUser; }
            set
            {
                _modifyUser = value;
                OnPropertyChanged("ModifyUser");
            }
        }
        private void _ModifyUser(object obj)
        {
            if (SelectedUser != null && SelectedUser.UserName != null)
            {
                StaticModels.vM_UserWindow_ModifyUser.UserName = SelectedUser.UserName;
                StaticModels.userWindow_ModifyUser = new Views.User.UserWindow_ModifyUser();
                StaticModels.userWindow_ModifyUser.ShowDialog();
            }
            else
            {
                Tips tips = new Tips("当前选择任何用户！");
                tips.ShowDialog();
            }
            
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        private ICommand _deleteUser;
        public ICommand DeleteUser
        {
            get { return _deleteUser; }
            set
            {
                _deleteUser = value;
                OnPropertyChanged("DeleteUser");
            }
        }
        private void _DeleteUser(object obj)
        {
            UserContext DeleteUser = new UserContext()
            {
                UserName = SelectedUser.UserName,
            };

            if (StaticModels.user.User.UserName != "" && StaticModels.user.User.UserName != null)
            {
                if (SelectedUser == null || SelectedUser.UserName == "")
                {
                    Tips tips = new Tips("未选择任何用户！");
                    tips.ShowDialog();
                    return;
                }
                if (SelectedUser.PowerLevel > StaticModels.user.User.PowerLevel) 
                {
                    Tips tips = new Tips("你没有权限删除高于你权限的用户！");
                    tips.ShowDialog();
                    return;
                }

                InputWindow inputWindow = new InputWindow("请输入该用户密码");
                inputWindow.ShowDialog();
                DeleteUser.Password = inputWindow.Result;
                if (StaticModels.user.GetUser_Password(DeleteUser).Rows.Count > 0)
                {
                    //添加日志
                    StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                    {
                        Context = "删除用户'" + DeleteUser.UserName + "'",
                        User = StaticModels.user.User.UserName
                    });
                    StaticModels.user.RemoveUser(DeleteUser);
                    Tips tips = new Tips("该用户已删除！");
                    tips.ShowDialog();
                }
                else
                {
                    //添加日志
                    StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                    {
                        Context = "尝试删除用户'" + DeleteUser.UserName + "'，但密码输入错误",
                        User = StaticModels.user.User.UserName
                    });
                    Tips tips = new Tips("密码输入错误！");
                    tips.ShowDialog();
                }

            }
            else
            {
                Tips tips = new Tips("未登录任何用户！");
                tips.ShowDialog();
            }
            //刷新现有用户
            UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);
        }
        #endregion
    }

    public class User
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        private string _userName;
        /// <summary>
        /// 权限
        /// </summary>
        public string Power
        {
            get { return _power; }
            set 
            { 
                _power = value; 
                switch(value)
                {
                    case "管理员":
                        PowerLevel = 4000;
                        break;
                    case "工程师":
                        PowerLevel = 3000;
                        break;
                    case "操作员":
                        PowerLevel = 2000;
                        break;
                    case "游客":
                        PowerLevel = 1000;
                        break;
                    default:
                        PowerLevel = 0;
                        break;
                }
            }
        }
        /// <summary>
        /// 权限等级
        /// </summary>
        public int PowerLevel;
        private string _power;
        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks
        {
            get { return _remarks; }
            set { _remarks = value; }
        }
        private string _remarks;
    }
}
